#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 2e5 + 10;

int n, k;
string s;

void solve(){
    cin >> n >> k;
    cin >> s;


    int minv = 0, hh = 0, tt = k - 1;
    
    for(int i = 0; i < k; i ++){
        if(s[i] == 'W'){
            minv ++;
        }
    }

    int curv = minv;

    while(tt + 1 < n){
        if(s[tt + 1] == 'W') curv ++;
        if(s[hh] == 'W') curv --;

        minv = min(minv, curv);
        tt++, hh ++;
    }

    cout << minv << '\n';
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        solve();
    }

    return 0;
}